package dao;

import model.User;
import java.sql.*;

/**
 * 用户数据访问对象
 */
public class UserDao {
    // 根据用户名和密码查找用户
    public User findByAccountAndPassword(String account, String password) {
        String sql = "SELECT * FROM users WHERE 账号 = ? AND 密码 = ?";
        try (Connection conn = BaseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, account);
            pstmt.setString(2, password);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return mapUser(rs);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 根据ID查找用户
    public static User getById(int id) {
        String sql = "SELECT * FROM users WHERE id = ?";
        try (Connection conn = BaseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return mapUser(rs);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 根据账户查找用户
    public User findByAccount(String account) {
        String sql = "SELECT * FROM users WHERE 账号 = ?";
        try (Connection conn = BaseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, account);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return mapUser(rs);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 添加用户
    public boolean addUser(User user) {
        String sql = "INSERT INTO users (账号, 密码, 姓名, 身份证号, 手机号, 银行卡号, 是否为管理员, 年龄) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = BaseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, user.getAccount());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getName());
            pstmt.setString(4, user.getIdCardNo());
            pstmt.setString(5, user.getMobileNo());
            pstmt.setString(6, user.getBankCardNo());
            pstmt.setBoolean(7, user.isAdmin());
            pstmt.setInt(8, user.getAge());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 将结果集映射为用户对象
    private static User mapUser(ResultSet rs) throws SQLException {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setAccount(rs.getString("账号"));
        user.setPassword(rs.getString("密码"));
        user.setName(rs.getString("姓名"));
        user.setIdCardNo(rs.getString("身份证号"));
        user.setMobileNo(rs.getString("手机号"));
        user.setBankCardNo(rs.getString("银行卡号"));
        user.setAdmin(rs.getBoolean("是否为管理员"));
        user.setAge(rs.getInt("年龄"));
        return user;
    }
}